package hevs.activities;

import hevs.greencontrol.R;
import hevs.greencontrol.objects.DrawView;
import hevs.greencontrol.objects.ImageAdapter;
import hevs.greencontrol.objects.DrawView.PlanMode;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class RoomPlan extends Activity implements OnClickListener{

	private DrawView drawView ;
	private GridView gridview;
	private Menu menu;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{	
		super.onCreate(savedInstanceState);


		drawView = new DrawView(this);
		setContentView(R.layout.roomplan);

		FrameLayout drawViewFrame = (FrameLayout) findViewById(R.id.drawview);
		drawViewFrame.addView(drawView);

		gridview = (GridView) findViewById(R.id.gridview);
		gridview.setAdapter(new ImageAdapter(this));
		
		
		

		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				
				switch(position)
				{
					case 0 : drawView.setMode(PlanMode.EDIT_SELECT); break;
					case 1 : drawView.setMode(PlanMode.EDIT_RECTANGLE); break;
					case 2 : drawView.setMode(PlanMode.EDIT_CUSTOM); break;
					case 3 : drawView.deleteSelectedRooms(); break;
					
					default : drawView.setMode(PlanMode.NORMAL);
				}
	
			}
		});
		
		if(drawView.getMode() == PlanMode.NORMAL)
		{
			gridview.setVisibility(4);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
			
	}

	//***********************************************************************************************  
	// Option menu :
	//*********************************************************************************************** 

	/**
	 * Create the option menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		if(this.menu == null)
		{
			this.menu = menu;
		}

		if(drawView.getMode() == PlanMode.NORMAL)
		{
			gridview.setVisibility(4);
		}
		else
		{
			gridview.setVisibility(0);
		}
		
		if(drawView.getMode() == PlanMode.NORMAL)
		{
			menu.clear();
			menu.add(0, 1000, 0, "Edit").setIcon(android.R.drawable.ic_media_play) ;
		}
		else
		{
			menu.clear();
			menu.add(0, 1001, 0, "Normal").setIcon(android.R.drawable.ic_media_play) ;
		}	
		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * Switch the actions when executed on menu item click
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case 1000:
			// Normal mode :
			drawView.setMode(PlanMode.EDIT_SELECT);
			onCreateOptionsMenu(menu);
			break;

		case 1001:
			drawView.setMode(PlanMode.NORMAL);
			onCreateOptionsMenu(menu);
			break;

		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}


}

